"""
地图界面
"""
import threading
import time

import qdarkstyle
from PyQt5.QtCore import QUrl, QObject, pyqtSignal, Qt
from PyQt5.QtWebChannel import QWebChannel
from PyQt5.QtWidgets import QWidget, QPushButton, QHBoxLayout, QSpacerItem, QSizePolicy, QCheckBox, \
    QVBoxLayout
from PyQt5.QtWebEngineWidgets import *
from lib import db
import Simulation


class Interact_list(QObject):
    # 定义信号,该信号会在js中绑定一个js方法.
    list_send_to_js = pyqtSignal(list)

    def __init__(self, parent=None):
        super().__init__(parent)
        # 交互对象接收到js调用后执行的回调函数.
        self.receive_str_from_js_callback = None


class Form2(QWidget, Simulation.Simulation_cfg):
    def __init__(self):
        super().__init__()

        self.show_bus = False  # Flag of showing bus or not
        self.db = db.database()  # 创建一个数据库对象

        self.btnPress1 = QPushButton("地图展示")
        self.btnPress2 = QPushButton("浏览线路")
        self.btnPress3 = QPushButton("使用帮助")
        self.btnPress4 = QPushButton("了解更多")
        self.btnPress1.setFixedSize(170, 60)
        self.btnPress2.setFixedSize(170, 60)
        self.btnPress3.setFixedSize(170, 60)
        self.btnPress4.setFixedSize(170, 60)

        qss_style = ''' 
                                     QPushButton{
                                            text-align : center;
                                            font-family:Microsoft Yahei;
                                            font-size:12pt;
                                            background-color : #F5F5F5;
                                            border-color: gray;
                                            border-width: 2px;
                                            border-radius: 10px;
                                            padding: 1px;
                                            height : 14px;
                                            border-style: outset;
                                        }
                                         QPushButton:pressed{
                                            text-align : center;
                                            font-family:Microsoft Yahei;
                                            font-size:12pt;
                                            background-color : #ADD8E6;
                                            border-color: gray;
                                            border-width: 2px;
                                            border-radius: 10px;
                                            padding: 1px;
                                            height : 14px;
                                            border-style: outset;
                                        }
                                     '''
        self.btnPress1.setStyleSheet(qss_style)
        self.btnPress2.setStyleSheet(qss_style)
        self.btnPress3.setStyleSheet(qss_style)
        self.btnPress4.setStyleSheet(qss_style)

        self.checkbox1 = QCheckBox('显示公交站点', self)
        self.checkbox1.setStyleSheet(qdarkstyle.load_stylesheet_pyqt5())
        self.checkbox2 = QCheckBox('显示公交位置', self)
        self.checkbox3 = QCheckBox('显示路况信息', self)
        self.checkbox4 = QCheckBox('显示POI文字', self)
        self.checkbox5 = QCheckBox('显示POI图标', self)

        self.qssStyle_down = ''' 
                            text-align : center;
                            font-family:Microsoft Yahei;
                            font-size:12pt;   
                            background-color : #9ACD32;                      
                        '''
        self.qssStyle_up = ''' 
                            text-align : center;
                            font-family:Microsoft Yahei;
                            font-size:12pt;   
                            background-color : #D3D3D3;                               
                        '''
        self.checkbox1.setStyleSheet(self.qssStyle_up)
        self.checkbox2.setStyleSheet(self.qssStyle_up)
        self.checkbox3.setStyleSheet(self.qssStyle_up)
        self.checkbox4.setStyleSheet(self.qssStyle_down)
        self.checkbox5.setStyleSheet(self.qssStyle_down)


        self.checkbox4.setChecked(True)
        self.checkbox5.setChecked(True)

        self.browser = QWebEngineView()
        self.browser.load(QUrl("https://li_hong_chen.gitee.io/bus_project/html/index.html"))

        # qt与js通信
        self.web_channel = Interact_list()
        channel = QWebChannel(self.browser.page())
        channel.registerObject('Web_Channel', self.web_channel)
        self.browser.page().setWebChannel(channel)

        tabel_layout = QHBoxLayout()  #
        btn_layout = QVBoxLayout()
        t_spaceItem = QSpacerItem(300, 20, QSizePolicy.Expanding, QSizePolicy.Expanding)
        btn_layout.addWidget(self.btnPress1)
        btn_layout.addWidget(self.btnPress2)
        btn_layout.addWidget(self.btnPress3)
        btn_layout.addWidget(self.btnPress4)
        btn_layout.addWidget(self.checkbox1)
        btn_layout.addWidget(self.checkbox2)
        btn_layout.addWidget(self.checkbox3)
        btn_layout.addWidget(self.checkbox4)
        btn_layout.addWidget(self.checkbox5)
        btn_layout.addStretch()
        # btn_layout.addItem(t_spaceItem)
        tabel_layout.addItem(btn_layout)
        tabel_layout.addWidget(self.browser)
        tabel_layout.setStretchFactor(btn_layout, 1)
        tabel_layout.setStretchFactor(self.browser, 11)

        self.setLayout(tabel_layout)

        self.btnPress1.clicked.connect(self.btnPress1_Clicked)
        self.btnPress2.clicked.connect(self.btnPress2_Clicked)
        self.btnPress3.clicked.connect(self.btnPress3_Clicked)
        self.btnPress4.clicked.connect(self.btnPress4_Clicked)

        self.checkbox1.stateChanged.connect(self.change_checkbox1)
        self.checkbox2.stateChanged.connect(self.change_checkbox2)
        self.checkbox3.stateChanged.connect(self.change_checkbox3)
        self.checkbox4.stateChanged.connect(self.change_checkbox4)
        self.checkbox5.stateChanged.connect(self.change_checkbox5)

    def btnPress1_Clicked(self):
        self.btnPress1.setDown(True)
        self.btnPress2.setDown(False)
        self.btnPress3.setDown(False)
        self.btnPress4.setDown(False)
        self.web_channel = Interact_list()
        channel = QWebChannel(self.browser.page())
        channel.registerObject('Web_Channel', self.web_channel)
        self.browser.page().setWebChannel(channel)
        self.browser.load(QUrl("http://127.0.0.1:8080/html/index.html"))
        self.browser.load(QUrl("https://li_hong_chen.gitee.io/bus_project/html/index.html"))

    def btnPress2_Clicked(self):
        self.btnPress1.setDown(False)
        self.btnPress2.setDown(True)
        self.btnPress3.setDown(False)
        self.btnPress4.setDown(False)
        self.browser.load(QUrl("http://127.0.0.1:8080/html/track.html"))
        self.browser.load(QUrl("https://li_hong_chen.gitee.io/bus_project/html/track.html"))

    def btnPress3_Clicked(self):
        self.btnPress1.setDown(False)
        self.btnPress2.setDown(False)
        self.btnPress3.setDown(True)
        self.btnPress4.setDown(False)
        self.browser.load(QUrl("http://8.140.108.34/index.php/archives/7/"))

    def btnPress4_Clicked(self):
        self.btnPress1.setDown(False)
        self.btnPress2.setDown(False)
        self.btnPress3.setDown(False)
        self.btnPress4.setDown(True)
        self.browser.load(QUrl("https://gitee.com/li_hong_chen/bus_project"))

    def change_checkbox1(self):
        """
        显示/关闭：公交车站
        """
        if self.checkbox1.checkState() == Qt.Checked:
            self.checkbox1.setStyleSheet(self.qssStyle_down)
            mess = self.db.Read_Station()
            station_data = []
            for item in mess:
                station_data.append(list(item))
            station_data.append(['station', 1])
            self.web_channel.list_send_to_js.emit(station_data)
        if self.checkbox1.checkState() == Qt.Unchecked:
            self.checkbox1.setStyleSheet(self.qssStyle_up)
            mess = self.db.Read_Station()
            station_data = []
            for item in mess:
                station_data.append(list(item))
            station_data.append(['station', 0])
            self.web_channel.list_send_to_js.emit(station_data)

    def change_checkbox2(self):
        """v
        显示/关闭：公交车
        """
        if self.checkbox2.checkState() == Qt.Checked:
            self.checkbox2.setStyleSheet(self.qssStyle_down)
            self.show_bus = True
            t1 = threading.Thread(target=self.Time_Update_Bus)
            t1.setDaemon(True)
            t1.start()

        elif self.checkbox2.checkState() == Qt.Unchecked:
            self.checkbox2.setStyleSheet(self.qssStyle_up)
            self.show_bus = False
            mess = self.db.Read_Station()
            bus_data = []
            for item in mess:
                bus_data.append(list(item))
            bus_data.append(['bus', 0])
            self.web_channel.list_send_to_js.emit(bus_data)

    def change_checkbox3(self):
        """
        显示/关闭：路况
        """
        if self.checkbox3.checkState() == Qt.Checked:
            self.checkbox3.setStyleSheet(self.qssStyle_down)
            self.web_channel.list_send_to_js.emit([['road_flow', 1]])
        elif self.checkbox3.checkState() == Qt.Unchecked:
            self.checkbox3.setStyleSheet(self.qssStyle_up)
            self.web_channel.list_send_to_js.emit([['road_flow', 0]])

    def change_checkbox4(self):
        """
        显示/关闭：POI文字
        """
        if self.checkbox4.checkState() == Qt.Checked:
            self.checkbox4.setStyleSheet(self.qssStyle_down)
            self.web_channel.list_send_to_js.emit([['POI_words', 1]])
        elif self.checkbox4.checkState() == Qt.Unchecked:
            self.checkbox4.setStyleSheet(self.qssStyle_up)
            self.web_channel.list_send_to_js.emit([['POI_words', 0]])

    def change_checkbox5(self):
        """
        显示/关闭：POI图标
        """
        if self.checkbox5.checkState() == Qt.Checked:
            self.checkbox5.setStyleSheet(self.qssStyle_down)
            self.web_channel.list_send_to_js.emit([['POI_icon', 1]])
        elif self.checkbox5.checkState() == Qt.Unchecked:
            self.checkbox5.setStyleSheet(self.qssStyle_up)
            self.web_channel.list_send_to_js.emit([['POI_icon', 0]])

    def Time_Update_Bus(self):
        mess = None
        last_mess = mess
        while self.show_bus:

            # 首先清空原先公交车
            if last_mess is not None:
                bus_data = []
                for item in last_mess:
                    bus_data.append(list(item))
                bus_data.append(['bus', 0])
                self.web_channel.list_send_to_js.emit(bus_data)

            last_mess = mess
            mess = self.db.Read_Bus()
            bus_data = []
            for item in mess:
                bus_data.append(list(item))
            bus_data.append(['bus', 1])
            self.web_channel.list_send_to_js.emit(bus_data)
            time.sleep(self.Flag_map_update_time)


